package com.dream.keigezhushou.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RatingBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dream.keigezhushou.Activity.Uiutils.UiUtils;
import com.dream.keigezhushou.Activity.bean.KTVInfo;
import com.dream.keigezhushou.R;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

import static com.dream.keigezhushou.Activity.Uiutils.UiUtils.inflateView;

public class NearKtvActivity extends BaseActivity {

    @BindView(R.id.back)
    ImageView mBack;
    @BindView(R.id.textView)
    TextView mTextView;
    @BindView(R.id.zn)
    RelativeLayout mZn;
    @BindView(R.id.distance)
    RelativeLayout mDistance;
    @BindView(R.id.price)
    RelativeLayout mPrice;
    @BindView(R.id.list_lv)
    ListView mListLv;
    @BindView(R.id.activity_near_ktv)
    LinearLayout mActivityNearKtv;
    private ArrayList<KTVInfo> data = new ArrayList<>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_near_ktv);
        ButterKnife.bind(this);

        initData();

        mListLv.setAdapter(new MyAdapter(data));
        mListLv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                UiUtils.toast(String.valueOf(data.size()));

                UiUtils.toast("跳到KTV详情页");
                Intent intent = new Intent(UiUtils.getContext(), KtvDetailsActivity.class);//Ktv详情页面
//                intent.putExtra("position", position);
//             //   intent.putExtra("url", url);
                startActivity(intent);
            }
        });

    }
    private void initData(){
        for (int i=0;i<6;i++){
            KTVInfo info = new KTVInfo();
            info.setDistance("0.5km");
            info.setIv(getResources().getDrawable(R.mipmap.icon_iv));
            info.setLocation("未央路");
            info.setTitle("欢唱KTV");
            info.setPrivilege("39元/券，6小时欢唱+小吃饮品套餐");
            info.setOrder_detailsorder_details("开心包6小时68元，乐唱包6小时68元");
            info.setPirce("40");
            info.setPingfen("4");
            data.add(info);
        }


    }

    class MyAdapter extends BaseAdapter {


        public MyAdapter(ArrayList<KTVInfo> datalist) {
            data = datalist;
        }

        @Override
        public int getCount() {
            return data.size();
        }

        @Override
        public KTVInfo getItem(int position) {
            return data.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder=null;
            if(convertView==null){
                convertView= inflateView(R.layout.item_ktvbuy_fragment);
                holder = new ViewHolder();
                holder.iv = (ImageView) convertView.findViewById(R.id.image_iv);
                holder.title = (TextView) convertView.findViewById(R.id.ktv_title);
                holder.pingfen = (TextView) convertView.findViewById(R.id.pingfen);
                holder.bar = (RatingBar)convertView.findViewById(R.id.ratingBar);
                holder.pirce = (TextView) convertView.findViewById(R.id.price);
                holder.location = (TextView) convertView.findViewById(R.id.location);
                holder.distance = (TextView) convertView.findViewById(R.id.distance);
                holder.order_detailsorder_details = (TextView) convertView.findViewById(R.id.order_details);
                holder.privilege = (TextView) convertView.findViewById(R.id.privilege );
                convertView.setTag(holder);
            }else{
                holder = (ViewHolder) convertView.getTag();
            }

            KTVInfo info = getItem(position);
            holder.iv.setImageDrawable(info.getIv());
            holder.title.setText(info.getTitle());
            holder.pingfen.setText(info.getPingfen());
            holder.bar.setRating(Float.parseFloat(info.getPingfen()));
            holder.pirce.setText(info.getPirce());
            holder.location.setText(info.getLocation());
            holder.distance.setText(info.getDistance());
            holder.order_detailsorder_details.setText(info.getOrder_detailsorder_details());
            holder.privilege.setText(info.getPrivilege());


            return convertView;
        }
    }

    static class ViewHolder {
        ImageView iv;
        TextView title;
        TextView pirce;
        TextView pingfen;
        TextView location;
        TextView distance;
        TextView order_detailsorder_details;
        TextView privilege;
        RatingBar bar;
    }
}
